package com.gxa.community.agent.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.alibaba.fastjson.TypeReference;
import com.gxa.community.agent.service.AfterSalesService;
import com.gxa.community.agent.utils.RangeUtils;
import com.gxa.community.core.common.RequestUrl;
import com.gxa.community.core.pojo.vo.agent.qd.AgentOrderInfoVo;
import com.gxa.community.core.pojo.vo.order.AfterSalesVo;
import com.gxa.community.core.pojo.vo.order.OrderInfoVo;
import com.gxa.community.core.utils.R;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Service
public class AfterSalesServiceImpl implements AfterSalesService {
    /**
     * 查询售后申请列表
     */
    @Override
    public R queryAfterSales(Integer page, Integer size, AfterSalesVo afterSalesVo) {

        String post = RangeUtils.post(RequestUrl.ADMIN_URL.getUrl() + "/order/queryAfterSales", afterSalesVo, null, null);
        R r = JSONObject.parseObject(post, R.class);

        return !ObjectUtils.isEmpty(r) ? R.ok().put("data",r) : R.error("失败");
    }

    /**
     * 查询团长名下下单信息
     * @param page
     * @param size
     * @param o
     * @return
     */

    @Override
    public R queryOrder(Integer page, Integer size, OrderInfoVo o) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("page", page.toString());
        hashMap.put("size",size.toString());

        String post = RangeUtils.post(RequestUrl.ADMIN_URL.getUrl() + "/order/queryOrder", o, null, hashMap);

        R r = JSONObject.parseObject(post, R.class);
        JSONObject jsonObject = (JSONObject) r.get("data");

        Object eval = JSONPath.eval(jsonObject, "$.list");
        List<OrderInfoVo> orderInfoVos = JSONObject.parseObject(eval.toString(), new TypeReference<List<OrderInfoVo>>() {});

        List<AgentOrderInfoVo> list = new ArrayList<>();

        orderInfoVos.forEach(p ->
        {
            String[] split = p.getReceiverPhone().split("");
            for (int i = 0; i < split.length; i++) {
                if (i >= 3 && i <= 6)
                    split[i] = "*";
            }

            list.add(new AgentOrderInfoVo()
                    .setName(p.getUserInfo().getNickName())
                    .setPhone(String.join("", split))
                    .setTime(p.getCreateTime().toString())
                    .setNo(p.getNo())
                    .setPrice(p.getPrice())
                    .setPayment(p.getPayment()));
        });

        return !ObjectUtils.isEmpty(list) ? R.ok().put("data",list) : R.error("失败");
    }

    /**
     * 根据订单号查询商品被下单商品
     * @param orderNo
     * @return
     */
    @Override
    public R queryGoods(List<String> orderNo) {

        String post = RangeUtils.post(RequestUrl.ADMIN_URL.getUrl() + "/order/queryGoods", orderNo, null, null);
        R r = JSONObject.parseObject(post, R.class);

        return !ObjectUtils.isEmpty(r) ? R.ok().put("data",r) : R.error("失败");
    }

    /**
     * 新增售后
     * @param salesVo
     * @return
     */
    @Override
    public R addAfterSales(AfterSalesVo salesVo) {

        String post = RangeUtils.post(RequestUrl.ADMIN_URL.getUrl() + "/mine//addAfterSales", salesVo, null, null);
        R r = JSONObject.parseObject(post, R.class);

        return !ObjectUtils.isEmpty(r) ? R.ok().put("data",r) : R.error("失败");
    }
}
